/**
 * 
 * @authors kroyo
 * @date    2017-09-05 09:36:43
 * @version 1.0.0
 * 下拉列表
 */
datePicker('.date-wrap');
var $date = $('.date-wrap');
var $month = $('.date-wrap .month');

//下拉列表定义变量
var ondownArr = [true,true,true];

$('#web').on('click',function(e){
	var target = e.target.parentNode.parentNode;
	switch(true){
		//1.交易模式
		case hasClass(target,'tran-mode'):
			//1.关闭需求阶段
	    	closeSelist('.re-phase');
	    	//3.关闭品类
	    	closeSelist('.sel-cate');

	    	//关闭时间窗口
            $month.css('display','none');
			animated($date,false);
			ondate = true;
			changeDate();

			clickfun('#container-other .tran-mode .select','.tran-mode');
			break;
		//2.需求阶段	
		case hasClass(target,'re-phase'):
			//1.关闭交易模式
	    	closeSelist('.tran-mode');
	    	//3.关闭品类
	    	closeSelist('.sel-cate');

	    	//关闭时间窗口
            $month.css('display','none');
			animated($date,false);
			ondate = true;
			changeDate();

			clickfun('#container-other .re-phase .select','.re-phase');
			break;	
	    //3.日期控件		
		case hasClass(target,'tran-date'):
			//1.关闭交易模式
	    	closeSelist('.tran-mode');
	    	//2.关闭需求阶段
	    	closeSelist('.re-phase');
	    	//3.关闭品类
	    	closeSelist('.sel-cate');

	    	pudownList($date);
			break;	
		case hasClass(e.target.parentNode,'year'):
			$month.css('display','block');
			break;
	    //品类		
	    case hasClass(target,'sel-cate'):
	    	//1.关闭交易模式
	    	closeSelist('.tran-mode');
	    	//2.关闭需求阶段
	    	closeSelist('.re-phase');

			clickfun('#container-other .sel-cate .select','.sel-cate');

            //关闭时间窗口
            $month.css('display','none');
			animated($date,false);
			ondate = true;
			changeDate();

	    	break;			
	    default:
	    	//1.关闭交易模式
	    	closeSelist('.tran-mode');
	    	//2.关闭需求阶段
	    	closeSelist('.re-phase');
	    	//3.关闭品类
	    	closeSelist('.sel-cate');
	    	
	    	//关闭时间窗口
	    	$month.css('display','none');
			animated($date,false);
			ondate = true;
			changeDate();
	}
})
//2.事件委托 所需的判断 判断是否存在某个class
function hasClass(target,selector){
    var val = target.className.indexOf(selector);
    if(val>=0){
        return true;
    }else{
        return false;
    }
}

//1.下拉列表
function clickfun(sel,sel_wrap){
	var $select = $(sel);
	//显示选择视图
	var $text = $(sel+' .text');
	// 1.定义下拉列表框变量
	var $list = $(sel+' +.selectdown');
	// 2.定义下拉列表选项
	var $li = $(sel+' +.selectdown li');

    var i = returni(sel_wrap);
    var ondown = ondownArr[i];

	if(ondown){
		animated($list,ondown);
		$select.addClass('active');
		ondownArr[i] = false;
	}else{
		animated($list,ondown);
		$select.removeClass('active');
		ondownArr[i] = true;
	}
	//选择选项
	addActive($li,$text);
}
// 下拉列表选项
function addActive($li,$text){
	$li.on('click',function(){
		$li.removeClass('active');
		$(this).addClass('active');
		$text.val($(this).text());
	})
}
//收起下拉列表
function closeSelist(sel_wrap){
	var $select = $(sel_wrap+' .select');
	var $list = $(sel_wrap+' .selectdown');

	$select.removeClass('active');
	animated($list,false);

    ondownArr[returni(sel_wrap)] = true;

	return ondownArr[returni(sel_wrap)];
}

//判断并生成 i
function returni(sel){
	var i;
	if(sel=='.tran-mode'){
		i = 0;
	}else if (sel=='.re-phase') {
		i = 1;
	}else if(sel=='.sel-cate'){
		i = 2;
	}
	return i;
}

//下拉列表下拉与收缩的动画
function animated($list,off){
	if(off){
		$list.slideDown();
	}else{
		$list.slideUp();
	}
}


//2.时间日期列表
var dateArr = [];
var year,month,day ;
function datePicker(datesel){
	// 2.定义 year
	var $year = $(datesel+' .year');
	// 3.定义 month
	var $month = $(datesel+' .month');

	var nowyear = new Date().getFullYear();
	//时间年  1970-现在
	datelist($year,nowyear,1970,'年');
	//时间月  1-12月
	datelist($month,12,1,'月');


    scroll('.date-wrap .year');
    scroll('.date-wrap .month');
}
//生成日期时间选项 
function datelist($sel,max,min,text){
	for(var i = min;i<=max;i++){
		$sel.append('<li>'+i+text+'</li>');
	}
}
var ondate = true;
function pudownList($date){
	animated($date,ondate);
	if(ondate){
		ondate = false;
	}else{
		ondate = true;
		changeDate();
	}
}

//获取选择的时间日期
function selectDate(){
	//清空数组
	dateArr.length = 0;
	
	var $yearli = $('.date-wrap .year li');
	var $monthli = $('.date-wrap .month li');
	var $dateText = $('.tran-date .text');

	var textArr = [];

	for(var i = 0;i<$yearli.length;i++){
		if($yearli.eq(i).hasClass('active')){
			dateArr.push(i);
			textArr.push($yearli.eq(i).text());
			break;
		}
	}
	for(var i = 0;i<$monthli.length;i++){
		if($monthli.eq(i).hasClass('active')){
			dateArr.push(i);
			textArr.push($monthli.eq(i).text());
			break;
		}
	}
	$dateText.val(textArr[0]+textArr[1]);
	return dateArr;
}

clickAdd('.year');
clickAdd('.month');
function clickAdd(sel){
	var $dateli = $($('.date-wrap '+sel+' li'));
	$dateli.on('click',function(){
		$dateli.removeClass('active');
		$(this).addClass('active');
		if(sel=='.month'){
			selectDate();
		}
	})
}

//未保存,则回到原来状态
function changeDate(){
	var $list = $('#container-other .date-wrap li');
	$list.removeClass('active');
    //首先清空所有active
    if(dateArr.length>0){

        var a = dateArr[0],b= dateArr[1];

        var $year = $('#container-other .date-wrap .year');
        var $yearli = $('.date-wrap .year li');
		var $monthli = $('.date-wrap .month li');

        var height = $yearli.eq(a).height();

        $yearli.eq(a).addClass('active');
        $monthli.eq(b).addClass('active');
        $year.scrollTop(height*(a-3));
        $month.scrollTop(height*b);

        //展开所有选项卡
        $month.css('display','block');

        return true;
    }else{
    	$month.css('display','none');
    }
}

//判断当前月份的天数
function dayNum(year,mouth){
    var cond1 = year % 4 == 0;  //条件1：年份必须要能被4整除
    var cond2 = year % 100 != 0;  //条件2：年份不能是整百数
    var cond3 = year % 400 ==0;  //条件3：年份是400的倍数
    var cond = (cond1 && cond2) || cond3;
    if(mouth==1||mouth==3||mouth==5||mouth==7||mouth==8||mouth==10||mouth==12){
        day = 31;
    }
    if(mouth==4||mouth==6||mouth==9||mouth==11){
        day = 30;
    }
    if(cond){
        if(mouth==2){
            day = 29;
        }
    }else{
        if(mouth==2){
            day = 28;
        }
    }
    return day;
}



//滚动条初始化
function scroll(sel){
    $(sel).niceScroll({
        cursorcolor: "rgba(30,135,242,0.3)",//#CC0071 光标颜色
        cursoropacitymax: 1, //改变不透明度非常光标处于活动状态（scrollabar“可见”状态），范围从1到0
        touchbehavior: false, //使光标拖动滚动像在台式电脑触摸设备
        cursorwidth: "5px", //像素光标的宽度
        cursorborder: "0", // 	游标边框css定义
        cursorborderradius: "5px",//以像素为光标边界半径
        autohidemode: true //是否隐藏滚动条
    });
}